//
//  DCFeedBookViewController.m
//  Demo
//
//  Created by zhaolin on 2023/1/4.
//

#import "DCFeedBookViewController.h"



#define MaxLenth 160
@interface DCFeedBookViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UIButton *placeholderBtn;

@property (nonatomic, strong) UILabel *numLabel;

@end



@implementation DCFeedBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"FeedBook"];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
//    CGFloat imageWidth = _placeholderBtn.imageView.bounds.size.width;
//     CGFloat labelWidth = _placeholderBtn.titleLabel.bounds.size.width;
//    _placeholderBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0 , -labelWidth);
//    _placeholderBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0 , imageWidth);
}
- (void)configureUI {
    
    [super configureUI];
        
    UILabel *desLabel = [UILabel new];
    desLabel.text = @"Describe the problem encountered:";
    desLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    desLabel.font = DCFont_PF_Regular(13);
    [self.view addSubview:desLabel];
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.top.mas_equalTo(self.navgitBar.mas_bottom).mas_offset(10);
    }];
    
    UIView *colorBG = [UIView new];
    colorBG.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    colorBG.layer.cornerRadius = 8;
    colorBG.layer.masksToBounds = YES;
    [self.view addSubview:colorBG];
    [colorBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(desLabel.mas_bottom).mas_offset(14);
        make.height.mas_equalTo(202);
    }];
    
    _numLabel = [UILabel new];
    _numLabel.text = @"0/160";
    _numLabel.textColor = [UIColor colorWithString:@"#C7C7C7"];
    _numLabel.font = DCFont_PF_Regular(12);
    [colorBG addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-17);
    }];
    
    _textView = [[UITextView alloc] init];
    _textView.backgroundColor = [UIColor clearColor];
    _textView.delegate = self;
    _textView.textColor = [UIColor colorWithString:@"#1C221B"];
    _textView.font = DCFont_PF_Regular(15);
    [colorBG addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(11);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(15);
        make.bottom.mas_equalTo(_numLabel.mas_top);
    }];
    
    UIButton *placeholderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _placeholderBtn = placeholderBtn;
    [placeholderBtn setImage:[UIImage imageNamed:@"pleaseEnter"] forState:UIControlStateNormal];
    [placeholderBtn setTitle:@" Please enter" forState:UIControlStateNormal];
    [placeholderBtn setTitleColor:[UIColor colorWithString:@"#C7C7C7"] forState:UIControlStateNormal];
//    [placeholderBtn setTitleColor:[UIColor colorWithString:@"#1C221B"] forState:UIControlStateNormal];
    [placeholderBtn addTarget:self action:@selector(placeholderBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [colorBG addSubview:placeholderBtn];
    [placeholderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(23);
        make.top.mas_equalTo(20);
    }];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.layer.cornerRadius = 8;
    commitBtn.layer.masksToBounds = YES;
    [commitBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    commitBtn.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    commitBtn.titleLabel.font = DCFont_PF_Semibold(15);
    [commitBtn addTarget:self action:@selector(commintAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.right.mas_equalTo(-38);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(colorBG.mas_bottom).mas_offset(93);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length > MaxLenth) {
        NSString *text = textView.text;
        text = [text substringToIndex:MaxLenth];
        textView.text = text;
        self.numLabel.text = [NSString stringWithFormat:@"%ld/%d",textView.text.length,MaxLenth];
        [DCDCToast toastText:@"Over maximum length"];
    }
}


- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length) {
        self.placeholderBtn.hidden = YES;
    } else {
        self.placeholderBtn.hidden = YES;
    }
    self.numLabel.text = [NSString stringWithFormat:@"%ld/%d",textView.text.length,MaxLenth];
    
}

- (void)placeholderBtnAction:(UIButton *)sender {
    [self.textView becomeFirstResponder];
}

- (void)commintAction:(UIButton *)sender {
    [self.view endEditing:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCUserComplaintWithMessage:self.textView.text completeBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            [DCDCToast toastText:@"【Submit success】"];
            
            [self.navigationController popViewControllerAnimated:YES];

        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
